package com.mysteel.authserver.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mysteel.authserver.feign.MemberFeignService;
import com.mysteel.authserver.vo.WeiBoAccessTokenVo;
import com.mysteel.common.constant.AuthServerConstant;
import com.mysteel.common.utils.R;
import com.mysteel.common.vo.MemberResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * ProjectName: mail
 * ClassName: OAuth2Controller
 * Description:
 * date: 2021/03/09/009 18:56
 *
 * @author xiaochao
 */
@Controller
@RequestMapping("/OAuth2")
public class OAuth2Controller {


    @Autowired
    private MemberFeignService memberFeignService;

    @RequestMapping("/success")
    public String getAuthCode(@RequestParam("code") String code, HttpSession session){
        // 获取到code值，换取Access Token
        String baseUrl = "https://api.weibo.com/oauth2/access_token";
        // MultiValueMap<String,Object> requestParams = new LinkedMultiValueMap<>();
        Map<String,Object> requestParams = new HashMap<>(5);
        requestParams.put("client_id","2153747477");
        requestParams.put("client_secret","5c81196fd425650e411884d9f0d80c6d");
        requestParams.put("grant_type","authorization_code");
        requestParams.put("redirect_uri","http://auth.xcmail.com/OAuth2/success");
        requestParams.put("code",code);
        String response = HttpUtil.post(baseUrl, requestParams);
        //HttpEntity<MultiValueMap<String,Object>> httpEntity = new HttpEntity<>(requestParams);
        //ResponseEntity<String> response = restTemplate.postForEntity(baseUrl, httpEntity, String.class);
        /*if (response.getStatusCode().value() != 200){
            // 换取accessToken失败，跳转回登录页
            return "redirect:http://auth.xcmail.com/login.html";
        }
        String responseBody = response.getBody();*/
        if (StringUtils.isEmpty(response)){
            // 换取accessToken失败，跳转回登录页
            return "redirect:http://auth.xcmail.com/login.html";
        }
        WeiBoAccessTokenVo weiBoAccessTokenVo = JSONObject.parseObject(response, WeiBoAccessTokenVo.class);
        if (weiBoAccessTokenVo == null){
            // 换取accessToken失败，跳转回登录页
            return "redirect:http://auth.xcmail.com/login.html";
        }
        System.out.println(weiBoAccessTokenVo.getAccess_token());
        // 当前用户如果是第一次进入，则自动注册，否则进行登录
        R feignResult = memberFeignService.socialLogin(weiBoAccessTokenVo);
        if (feignResult.getCode() != 0){
            return "redirect:http://auth.xcmail.com/login.html";
        }
        MemberResponseVo memberResponseVo = feignResult.getData("data", new TypeReference<MemberResponseVo>() {});
        session.setAttribute(AuthServerConstant.LOGIN_USER_DATA,memberResponseVo);
        return "redirect:http://xcmail.com";
    }
}
